package cz.hanis.android.geography.xml;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import cz.hanis.android.geography.models.Country;

import android.util.Log;

public class CountryHandler extends DefaultHandler {

	private static final String COUNTRIES = "countries";
	private static final String COUNTRY = "country";
	private static final String COUNTRY_CODE = "countryCode";
	private static final String CONTINENT = "continent";
	//private static final String CAPITAL = "capital";
	private static final String CONTIGUOUS = "contiguous";
	
	//private static final String LANGUAGES = "languages";
	//private static final String LANGUAGE = "language";
	private static final String AREA = "areaInSqKm";
	private static final String POPULATION = "population";
	//private static final String TOTAL = "total";
	//private static final String RANK = "rank";
	//private static final String DENSITY = "density";
	//private static final String CURRENCY = "currency";
	//private static final String REGION= "region";
	//private static final String TLD = "tld";
	//private static final String GOVERNMENT = "government";
	//private static final String ISO_CODE = "iso3166";
	
	


	private final Stack<String> tagsStack = new Stack<String>();
	String value = null;

	private List<Country> countryList;
	private Country country;

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		String parentTag = peekTag();		
		pushTag(qName);
		
		if (localName.equals(CountryHandler.COUNTRIES)) {
			countryList = new ArrayList<Country>();
		} else if (localName.equals(CountryHandler.COUNTRY)) {
            if (COUNTRIES.equals(parentTag)) {
            	country = new Country();
            } 												
		}
		
	}
    
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

        String tag = peekTag();
        //System.out.println("T:" + tag);
        if (!qName.equals(tag)) {
            throw new InternalError();
        }

        popTag();
        String parentTag = peekTag();		

//        <countryCode>GQ</countryCode>
//        <geonameId>2309096</geonameId>
//        <isoNumeric>226</isoNumeric>
//        <fipsCode>EK</fipsCode>
//        <isoAlpha3>GNQ</isoAlpha3>
//        <continent>AF</continent>
//        <areaInSqKm>28051.0</areaInSqKm>
//        <population>1014999</population>
//        <languages>XAF</languages>
//        <west>9.346865</west>
//        <north>2.346989</north>
//        <east>11.335724</east>
//        <south>0.92086</south>
//        <contiguous>
//          <countryCode>CM</countryCode>
//          <countryCode>GA</countryCode>
//        </contiguous>
//      </country>        
        
        
		if (localName.equals(CountryHandler.COUNTRY)) {           
            	countryList.add(country);
//            } else if (CONTIGUOUS.equals(parentTag)) {
//                country.addContiguousCountry(value);
            						
		} else if (localName.equals(CountryHandler.COUNTRY_CODE)) {
			if (COUNTRY.equals(parentTag)) {
				country.setId(value);
            } else if (CONTIGUOUS.equals(parentTag)) {
              country.addContiguousCountryId(value);
            }														
		} else if (localName.equals(CountryHandler.CONTINENT)) {
			country.setContinentId(value);
//		} else if (localName.equals(CountryHandler.CAPITAL)) {
//			country.setCapital(value);
		} else if (localName.equals(CountryHandler.AREA)) {
			try {	
				country.setArea(Double.valueOf(value).intValue());
			} catch (NumberFormatException ex) {
				country.setArea(0);
			}
		} else if (localName.equals(CountryHandler.POPULATION)) {
			country.setPopulation(Double.valueOf(value).intValue());
		}		
		
		
//		else if (localName.equals(CountryHandler.LANGUAGE)) {
//			country.addLanguage(value);
//		} else if (localName.equals(CountryHandler.CURRENCY)) {
//			country.setCurrency(new Currency(value));
//		} else if (localName.equals(CountryHandler.REGION)) {
//			country.setRegion(value);
//		} else if (localName.equals(CountryHandler.GOVERNMENT)) {
//			country.setGovernment(value);
//		} else if (localName.equals(CountryHandler.TLD)) {
//			country.setTld(value);
//		} else if (localName.equals(CountryHandler.ISO_CODE)) {
//			country.setIsoCode(value);
//		} else if (localName.equals(CountryHandler.TOTAL)) {
//			if (POPULATION.equals(parentTag)) {
//				country.setPopulation(Integer.valueOf(value));
//            } else if (AREA.equals(parentTag)) {
//            	country.setArea(Integer.valueOf(value));
//            }
//		} else if (localName.equals(CountryHandler.RANK)) {
//			if (POPULATION.equals(parentTag)) {
//				country.setPopulationRank(Integer.valueOf(value));
//            } else if (AREA.equals(parentTag)) {
//            	country.setAreaRank(Integer.valueOf(value));
//            }
//		} else if (localName.equals(CountryHandler.DENSITY)) {
//			if (POPULATION.equals(parentTag)) {
//				country.setPopulationDensity(Double.valueOf(value));
//            } else if (AREA.equals(parentTag)) {
//            	country.setArea(Integer.valueOf(value));
//            }
//		}


	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		value = new String(ch, start, length);
	}

	public List<Country> getCountryList() {
		return countryList;
	}

	private void pushTag(String tag) {
		tagsStack.push(tag);
	}

	private String popTag() {
		return tagsStack.pop();
	}

	private String peekTag() {
		if(tagsStack.isEmpty()) {
			return null;
		}
		return tagsStack.peek();
	}

}
